#!/usr/bin/env python
# Molten Budget System  V0.4
# A simple monthly expense  / budget calculator by: Ian (Inc) Inc@inc-corporate.org
# http://code.inc-corporate.org/python
# mikeV/IanC 2006
#
# Change log
# 0.1 - First write of simple calculations
# 0.2 - Rewrite adding Export to file
# 0.3 - Complete Rewrite  Improving Speed
# 0.4 - Gui Being Added. Application Name Change
####################################################################################################
import sys, math, time
from Tkinter import *
print """
		Molten Budget System V0.4
		\\ \\ \\ \\ \\ \\ \\
			by:
				Ian (Inc)
		\\ \\ \\ \\ \\ \\ \\
"""
#Gui Portion of this Code.
root = Tk()
root.title("Molten Budget System V0.4")
root.geometry("200x100")
app = Frame(root)
app.grid()
lbl = Label(app, text = "Welcome to molten Budget System V0.4")
lbl.grid()
root.mainloop()

# End Gui Portion of this Code.


budget_items = {
	'rent':'Rent / mortgage payment: ',
	'car':'Car Payment: ',
	'food':'Amount spent on food: ',
	'spending':'Amount That you set aside for spending money: ',
	'savings':'mount That Deposit in a savings account or set aside to save: ',
	'budget':"Amount that you've budgeted for Each month in total for everything: "
}
budget_amounts = {}

text_file = None
try:
	text_file=open('file.txt','a')
except:
	print "Could not open 'file.txt'"
	sys.exit(1)

total = 0.00

print "Please enter the amounts below that you spent for this Month:"
for item in budget_items.keys():
	try:
		input = float(raw_input(budget_items[item]))
		budget_amounts[item] = input
		total+=input
	except:
		print "You did not enter a number, skipping item"

if "budget" not in budget_amounts.keys():
	print "Error: You must enter a budget amount!"
	sys.exit(1)

difference = math.fabs(budget_amounts['budget'] - total)
message=None

if total <= budget_amounts['budget']:
	message="Great job you are within your budget, you are under your budget by $%s " % str(difference)
else:
	message="You Went over your budget by $%s :(" % str(difference)

text_file.write("#Budget record for '%s'#\n" % time.strftime("%a, %d %b %Y %H:%M:%S"))

for item in budget_amounts.keys():
	text_file.write("\t%s = %s\n" % (item,budget_amounts[item]))
	
text_file.write(message+"\n")
text_file.write("\n# end buget record #\n")
print message
raw_input("\n\nHave a nice day! At any point you can Press the enter key to exit.")

text_file.close()